import { InjectionKey } from 'vue'
import { store } from 'quasar/wrappers'
import {
  createStore,
  Store as VuexStore,
  useStore as vuexUseStore
} from 'vuex'
import workbench from './workbench'
import tool from './tool'
import widget from './widget'
import km from './km'
import state, { StateInterface } from './state'
import actions from './actions'
import mutations from './mutations'
import getters from './getters'

// provide typings for `this.$store`
declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $store: VuexStore<StateInterface>
  }
}

// provide typings for `useStore` helper
export const storeKey: InjectionKey<VuexStore<StateInterface>> = Symbol('vuex-key')

export default store(function (/* { ssrContext } */) {
  const Store = createStore<StateInterface>({
    modules: {
      workbench,
      tool,
      widget,
      km
    },
    state,
    getters,
    mutations,
    actions,
    // enable strict mode (adds overhead!)
    // for dev mode and --debug builds only
    strict: !!process.env.DEBUGGING
  })
  return Store
})

export function useStore () {
  return vuexUseStore(storeKey)
}
